#!/bin/sh

# Slackware build script for epsxe

# Copyright (c) 2009, soyalexman, soyalexman@soyalexman.com
# PSX Controller icon made by soyalexman.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1.- Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# NOTE:plugins,bios or cfg are not considered on this script

PRGNAM=epsxe
VERSION=1.60
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

EPSXEZIP=160lin

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

set -e

rm -rf $PKG
mkdir -p $PKG/usr/games/$PRGNAM-$VERSION $OUTPUT
unzip $CWD/$PRGNAM$EPSXEZIP.zip -d $PKG/usr/games/$PRGNAM-$VERSION
cd $PKG/usr/games/$PRGNAM-$VERSION
chown -R root:root .

## We'll diverge from upstream a bit in locations but not functionality
## Some of this stuff needs to be writable by users, so it can't be on the
## /usr partition in case it's mounted readonly.  Also, we don't want *all* 
## users to have write permissions on it, so we'll handle that too.
# First, the global configuration file
mkdir -p $PKG/var/games/$PRGNAM
sed "s%@INSTALLDIR@%/usr/games/$PRGNAM-$VERSION%g" $CWD/dot.epsxerc > \
  $PKG/var/games/$PRGNAM/.epsxerc.new
cd $PKG/usr/games/$PRGNAM-$VERSION
  # Link the config file to where it's expected
  ln -s ../../../var/games/$PRGNAM/.epsxerc .epsxerc
  mv bios cfg cheats memcards plugins sstates snap \
    $PKG/var/games/$PRGNAM
  for i in bios cfg cheats memcards plugins sstates snap ; do 
    ln -s ../../../var/games/$PRGNAM/$i . ;
  done
cd -

# Make the plugins, bios, cfg, cheats, memcards, and snap dirs be owned by
# root:games and mode 2775 ; also make files 0664
chown -R root:games $PKG/var/games/$PRGNAM
find $PKG/var/games/$PRGNAM -type f -exec chmod 0664 {} \;
find $PKG/var/games/$PRGNAM -type d -exec chmod 2775 {} \;

# Add a wrapper in PATH (/usr/games) which will run the real executable at
# /usr/games/$PRGNAM-$VERSION/epsxe after handling some config details
sed "s%@INSTALLDIR@%/usr/games/$PRGNAM-$VERSION%g" $CWD/wrapper.epsxe \
  > $PKG/usr/games/epsxe
chmod 0755 $PKG/usr/games/epsxe

# Add a .desktop file and icon for good menu integration
mkdir -p $PKG/usr/share/{applications,pixmaps}
install -m 0644 $CWD/epsxe.desktop $PKG/usr/share/applications/epsxe.desktop
install -m 0644 $CWD/epsxe.png $PKG/usr/share/pixmaps/epsxe.png

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
mv docs/* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
( cd $PKG/usr/games/$PRGNAM-$VERSION 
  rm -rf docs ; ln -sf ../../doc/$PRGNAM-$VERSION/ docs
)

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
